
// 当前选中的电影分类_id（方便后续对电影数据进行筛选）
let currentTypeId = "";

showMovieType();
showMovieList();

function showMovieType() {
    getMovieTypeData()
        .then((res) => {
            movieTypeRender(res.data);
        })
        .catch((err) => {
            console.log('请求出错', err);
        })
}

function movieTypeRender(data) {
    data.unshift({
        _id: '',
        name: '全部'
    });
    // 对 data 进行渲染
    movieTypeEle.innerHTML = data.map(item => {
        if (item._id === currentTypeId) {
            return `<span class="active" data-id="${item._id}">${item.name}</span>`
        }
        return `<span data-id="${item._id}">${item.name}</span>`
    }).join('')
}

movieTypeEle.onclick = function (e) {
    if (e.target.nodeName == 'SPAN') {
        currentTypeId = e.target.dataset.id;
        showMovieType();
    }
}


function showMovieList() {
    getMovieData({
        pageSize: 30,
        currentPage: 1
    })
        .then((res) => {
            console.log('请求成功的结果', res)
            movieListRender(res.data.rows);
        })
        .catch((err) => {
            console.log('请求出错', err);
        })
}

function movieListRender(data) {
    // 对 data 进行渲染
    movieList.innerHTML = data.map(item => {
        return (
            `
            <div class="movie-item">
                <div class="movie-image" style="background-image: url(${item.cover_image})">
                    <div class="movie-info">
                        
                    </div>
                </div>
                <p class="movie-name">${item.name}</p>
                <em class="movie-rate">${item.score}</em>
            </div>
            `
        )
    }).join('')
}